package gui;

import javax.swing.*;
import javax.swing.text.AbstractDocument;
import java.awt.*;
import java.awt.event.*;

public class AdmCreate extends JPanel {
	
	private JLabel lbl_Intro;
	private JLabel lbl_Firstname;
	private JLabel lbl_Lastname;
	private JLabel lbl_CPRID;
	private JLabel lbl_PIN;
	private JLabel lbl_Header;
	private JTextField textField_Firstname;
	private JTextField textField_Lastname;
	private JTextField textField_CPRID;
	private JPasswordField passwordField_PIN;
	private JButton btn_Back;
	private JButton btn_Submit;
	private JTextField textField_Streetname;
	private JTextField textField_ZipCode;
	private JLabel lbl_Streetname;
	private JLabel lbl_Streetnr;
	private JTextField textField_Streetnr;
	private JLabel lbl_ZipCode;
	private JTextField textField_City;
	private JLabel lbl_City;
	private JLabel lbl_Note;
	private JTextField textField_Tlf;
	private JLabel lbl_Tlf;
	
	public AdmCreate() {
		
		setPreferredSize(new Dimension(550, 400));
		setLayout(null);
		
		lbl_Intro = new JLabel("Note: All fields are required in order to create an user");
		lbl_Intro.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Intro.setBounds(80, 65, 361, 21);
		add(lbl_Intro);
		
		lbl_Firstname = new JLabel("Firstname:");
		lbl_Firstname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Firstname.setBounds(241, 97, 82, 21);
		add(lbl_Firstname);
		
		lbl_Lastname = new JLabel("Lastname:");
		lbl_Lastname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Lastname.setBounds(241, 135, 81, 21);
		add(lbl_Lastname);
		
		lbl_CPRID = new JLabel("ID:");
		lbl_CPRID.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_CPRID.setBounds(48, 99, 22, 21);
		add(lbl_CPRID);
		
		lbl_PIN = new JLabel("PIN:");
		lbl_PIN.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_PIN.setBounds(38, 135, 33, 21);
		add(lbl_PIN);
		
		lbl_Header = new JLabel("Create User");
		lbl_Header.setFont(new Font("SansSerif", Font.BOLD, 26));
		lbl_Header.setBounds(200, 20, 144, 34);
		add(lbl_Header);
		
		lbl_Streetname = new JLabel("Streetname:");
		lbl_Streetname.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Streetname.setBounds(230, 210, 93, 21);
		add(lbl_Streetname);
		
		lbl_Streetnr = new JLabel("Street nr.:");
		lbl_Streetnr.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Streetnr.setBounds(248, 243, 75, 21);
		add(lbl_Streetnr);
		
		lbl_ZipCode = new JLabel("Zip code:");
		lbl_ZipCode.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_ZipCode.setBounds(252, 279, 71, 21);
		add(lbl_ZipCode);
		
		lbl_City = new JLabel("City:");
		lbl_City.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_City.setBounds(286, 315, 36, 21);
		add(lbl_City);
		
		lbl_Note = new JLabel("<html><p>Note: ID and PIN <br> are required for login </p></html>");
		lbl_Note.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		lbl_Note.setBounds(38, 191, 185, 48);
		add(lbl_Note);
		
		lbl_Tlf = new JLabel("Telephone:");
		lbl_Tlf.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Tlf.setBounds(230, 167, 87, 21);
		add(lbl_Tlf);
		
		btn_Back = new JButton("Back");
		btn_Back.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Back.setBounds(38, 270, 100, 40);
		add(btn_Back);
		
		btn_Submit = new JButton("Submit");
		btn_Submit.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Submit.setBounds(369, 349, 108, 40);
		add(btn_Submit);
			
		textField_Firstname = new JTextField();
		textField_Firstname.addMouseListener(mouseListener);
		textField_Firstname.setToolTipText("characters only");
		textField_Firstname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Firstname.setBounds(326, 97, 150, 25);
		add(textField_Firstname);
		textField_Firstname.setColumns(10);
		
		textField_Lastname = new JTextField();
		textField_Lastname.addMouseListener(mouseListener);
		textField_Lastname.setToolTipText("characters only");
		textField_Lastname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Lastname.setBounds(326, 133, 150, 25);
		add(textField_Lastname);
		textField_Lastname.setColumns(10);
		
		textField_CPRID = new JTextField();
		textField_CPRID.addMouseListener(mouseListener);
		textField_CPRID.setToolTipText("10 digits only");
		((AbstractDocument)textField_CPRID.getDocument()).setDocumentFilter(new LimitOfChar(10));
		textField_CPRID.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_CPRID.setBounds(80, 97, 150, 25);
		add(textField_CPRID);
		textField_CPRID.setColumns(10);
		
		passwordField_PIN = new JPasswordField();
		passwordField_PIN.addMouseListener(mouseListener);
		passwordField_PIN.setToolTipText("4 digits only");
		((AbstractDocument)passwordField_PIN.getDocument()).setDocumentFilter(new LimitOfChar(4));
		passwordField_PIN.setFont(new Font("SansSerif", Font.PLAIN, 16));
		passwordField_PIN.setBounds(80, 133, 150, 25);
		add(passwordField_PIN);

		textField_Streetname = new JTextField();
		textField_Streetname.addMouseListener(mouseListener);
		textField_Streetname.setToolTipText("characters only");
		textField_Streetname.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Streetname.setBounds(326, 208, 150, 25);
		add(textField_Streetname);
		textField_Streetname.setColumns(10);
		
		textField_ZipCode = new JTextField();
		textField_ZipCode.addMouseListener(mouseListener);
		textField_ZipCode.setToolTipText("4 digits only");
		((AbstractDocument)textField_ZipCode.getDocument()).setDocumentFilter(new LimitOfChar(4));
		textField_ZipCode.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_ZipCode.setBounds(326, 277, 150, 25);
		add(textField_ZipCode);
		textField_ZipCode.setColumns(10);
		
		textField_Streetnr = new JTextField();
		textField_Streetnr.addMouseListener(mouseListener);
		textField_Streetnr.setToolTipText("digits only allowed");
		textField_Streetnr.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Streetnr.setBounds(326, 241, 150, 25);
		add(textField_Streetnr);
		textField_Streetnr.setColumns(10);
		
		textField_City = new JTextField();
		textField_City.addMouseListener(mouseListener);
		textField_City.setToolTipText("characters only");
		textField_City.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_City.setBounds(326, 313, 150, 25);
		add(textField_City);
		textField_City.setColumns(10);
		
		textField_Tlf = new JTextField();
		textField_Tlf.addMouseListener(mouseListener);
		textField_Tlf.setToolTipText("12 digits only allowed");
		((AbstractDocument)textField_Tlf.getDocument()).setDocumentFilter(new LimitOfChar(12));
		textField_Tlf.setFont(new Font("SansSerif", Font.PLAIN, 16));
		textField_Tlf.setBounds(326, 169, 150, 25);
		add(textField_Tlf);
		textField_Tlf.setColumns(10);
	}
		
	public void addActionListener(ActionListener event) {
		btn_Submit.addActionListener(event);
		btn_Back.addActionListener(event);
	}
	
	public JButton getBack(){
		return btn_Back;
	}
	
	public JButton getSubmit(){
		return btn_Submit;
	}
		
	public void clearFields(){
		textField_CPRID.setText("");
		passwordField_PIN.setText("");
		textField_Firstname.setText("");
		textField_Lastname.setText("");
		textField_Streetname.setText("");
		textField_Streetnr.setText("");
		textField_ZipCode.setText("");	
		textField_City.setText("");
		textField_Tlf.setText("");
	}

	public String getTextField_Firstname() {
		return textField_Firstname.getText();
	}

	public String getTextField_Lastname() {
		return textField_Lastname.getText();
	}

	public int getTextField_USERID() {
		return Integer.parseInt(textField_CPRID.getText().trim());
	}

	public int getPasswordField_USERPIN() {
		char[] pin = passwordField_PIN.getPassword();
		return Integer.parseInt(new String(pin));
	}

	public String getTextField_Streetname() {
		return textField_Streetname.getText();
	}

	public int getTextField_Zipcode() {
		return Integer.parseInt(textField_ZipCode.getText().trim());
	}

	public int getTextField_Streetnr() {
		return Integer.parseInt(textField_Streetnr.getText().trim());
	}
	
	public String getTextField_City() {
		return textField_City.getText();
	}
	
	public int getTextField_Tlf(){
		return Integer.parseInt(textField_Tlf.getText().trim());
	}
	
	MouseListener mouseListener = new MouseAdapter(){    
	    final int defaultTimeout = ToolTipManager.sharedInstance().getInitialDelay();

	    @Override
	    public void mouseEntered(MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(0);
	    }

	    @Override
	    public void mouseExited(MouseEvent e) {
	        ToolTipManager.sharedInstance().setInitialDelay(defaultTimeout);
	    }
	};
	
}
